package com.ikas.autumn.iec104.module.protocol.manual;

import com.ikas.autumn.iec104.common.baseEntity.JsonResult;
import com.ikas.autumn.iec104.common.exception.BusinessException;
//import com.ikas.autumn.iec104.common.util.RedisUtil;
import com.ikas.autumn.iec104.module.iec.apdumodel.Apdu;
import com.ikas.autumn.iec104.module.iec.apdumodel.Asdu;
import com.ikas.autumn.iec104.module.iec.asdudataframe.TotalSummonType;
import com.ikas.autumn.iec104.module.iec.container.Iec104Link;
import com.ikas.autumn.iec104.module.iec.container.LinkContainer;
import com.ikas.autumn.iec104.module.iec.nettyconfig.Master104Handle;
import com.ikas.autumn.iec104.module.iec.nettyconfig.TechnicalTerm;
import com.ikas.autumn.iec104.module.iec.utils.ByteUtil;
import com.ikas.autumn.iec104.module.iec.utils.DataConvertor;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelId;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * 手动发送104报文
 * @author wuyf
 * @date 2023/9/18 2:14
 */
@RestController
@RequestMapping("/manual/send")
public class ManualSend104Controller {

    /**
     * 发送测试报文
     * @param code 报文信息
     * @return JsonResult
     */
    @GetMapping("/code")
    public JsonResult testCode(@RequestParam String code) {
        Apdu apdu = new Apdu();
        byte[] bytes = ByteUtil.hexStringToBytes(code);
        ByteBuf buf = Unpooled.wrappedBuffer(bytes);
        try {
            Apdu apdu1 = apdu.loadByteBuf(buf);
            return JsonResult.ok(Master104Handle.getApduCustom(apdu1));
        } catch (Exception e) {
            throw new BusinessException("解析异常"+e.getMessage());
        }
    }

    /**
     * 发送总召唤
     * @return JsonResult
     */
    @GetMapping("/totalSummon")
    public JsonResult sendTotalSummon() {
        try {

            TotalSummonType totalSummonType =new TotalSummonType();
            Asdu asdu = totalSummonType.generateBack();
            Apdu apdu=new Apdu();
            apdu.setAsdu(asdu);
            Map<ChannelId, Iec104Link> links = LinkContainer.getInstance().getLinks();
            // 此处的逻辑修改是为了解决等保测评代码检测
            Collection<Iec104Link> values = links.values();
            ArrayList<Iec104Link> list = new ArrayList<>(values);
            Iec104Link link = list.get(0);
            int send = link.getISend();
            int receive = link.getIReceive();
            apdu.setSendSeqNum(send++);
            apdu.setReceiveSeqNum(receive);
            apdu.setApciType(Apdu.ApciType.I_FORMAT);
            link.setISend(send);
            byte[] bs = apdu.encode();
            link.getLog().info("发送总召唤帧：" + DataConvertor.byte2String(bs));
            link.getChannel().writeAndFlush(Unpooled.copiedBuffer(bs));
            LinkContainer.getInstance().getLinks().put(link.getChannel().id(), link);
            //总召唤批次号加1
//            RedisUtil.inc(TechnicalTerm.REDIS_TOTAL_SUMMON_KEY);

        } catch (Exception e) {
            throw new BusinessException("发送总召唤异常"+e.getMessage());
        }
        return JsonResult.ok("发送总召唤完成");
    }


}
